package cn.wegoteam.shop.adm.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import cn.wegoteam.shop.action.BaseAction;
import cn.wegoteam.shop.inter.ProductServiceInter;
import cn.wegoteam.shop.po.Product;

@ParentPackage("manage")
@Namespace("/")
@Controller
public class ManProductAction extends BaseAction<Product>{
	/**
	 * 
	 */
	private static final long serialVersionUID = -7214652284224332303L;
	@Autowired
	private ProductServiceInter productService;

	@Action(value = "manageProductList", results = { @Result(name = SUCCESS, location = MANAGE
			+ "list.jsp") })
	public String manageProductList() {
		//request.setAttribute("spcode", getParameter("spcode", ""));
		return SUCCESS;
	}

	@Action(value = "manageLoadProductList")
	public void loadProductList() {
		int pageSize = getIntParameter("pagesize");
		int currentPage = getIntParameter("page");
		pageBean.setSize(pageSize == 0 ? defaultSize : pageSize);
		pageBean.setCurrentPage(currentPage == 0 ? 1 : currentPage);
		pageBean.setTotalPage(getIntParameter("totalPage"));
		//writeStringToResponse(productService.getJsonList(pageBean,DataUtils.getMap("pcode", getParameter("spcode", "TOP")),null));
		writeStringToResponse(productService.getJsonList(pageBean,null,null,request));
	}

	@Action(value = "manageFindProductById", results = { @Result(name = SUCCESS, location = MANAGE
			+ "addProduct.jsp") })
	public String findProductById() {
		if (model.getId() != null)
			model = productService.get(model.getId());
		return SUCCESS;
	}

	@Action(value = "manageAddProduct")
	public String addProduct() {
		tipMessage = "不允许添加相同Key值的Product！";// 错误提示，操作成功时不会显示
			if(model.getId()==null)
				productService.saveOrUpdate(model);
			else
				productService.updateBySql(model);
			return OPTRESULTSUCCESS;
	}

	@Action(value = "manageDeleteProduct")
	public void deleteProduct() {
		writeStringToResponse(productService.delete(model));
	}

	/****************************************************** get() and set() ***********************************/


}
